/**
 * Copyright 2007-2008 Vidar Svansson
 *     
 * This file is part of JAdapter.
 * 
 * JAdapter is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * JAdapter is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with JAdapter.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.jadapter.registry;

import org.jadapter.Adapter;
import org.jadapter.Query;
import org.jadapter.Transformer;
import org.jadapter.graph.Graph;
import org.jadapter.graph.TransitiveClosure;

/**
 * Implementation of the AdapterRegistry interface that provides transitivity
 * support by retaining a transitive closure of transformation paths. 
 * <br></br>
 * <br></br>
 * 
 * 
 * @author Vidar Svansson
 * @since 0.1
 * @see TransientAdapterRegistry
 */
public class TransitiveAdapterRegistry extends AbstractAdapterRegistry
		implements InterfaceProviderRegistry {

	private QueryAdapterContainer container;

	public TransitiveAdapterRegistry() {
		this(new TransitiveClosure<Class<?>, Adapter<Object, Object>>(),
				new TransitiveClosure<Class<?>, Adapter<Object, Object>>());
	}

	public TransitiveAdapterRegistry(Adapter<?, ?>... adapters) {
		this(new TransitiveClosure<Class<?>, Adapter<Object, Object>>(),
				new TransitiveClosure<Class<?>, Adapter<Object, Object>>(),
				adapters);
	}

	public TransitiveAdapterRegistry(
			TransitiveClosure<Class<?>, Adapter<Object, Object>> transitiveClosure,
			TransitiveClosure<Class<?>, Adapter<Object, Object>> transitiveClosure2,
			Adapter<?, ?>... adapters) {
		container = new QueryAdapterContainer(transitiveClosure,
				transitiveClosure2);
		container.register(adapters);
	}

	@SuppressWarnings("unchecked")
	public <T> T transform(final Object context, final Class<T> type) {

		Query<Adapter<Object, Object>> adapters = container.findAdapter(
				context, type);

		Object t = context;
		for (Transformer<Object, Object> adapter : adapters) {
			t = adapter.transform(t);
		}

		return (T) t;
	}

	/**
	 * Set the adapters stored in this registry. This removes any adapters
	 * already registered. use
	 * {@link AdapterRegistry#registerAdapters(java.lang.Iterable)} to add
	 * adapters.
	 * 
	 * @param adapters
	 *            represented by this registry
	 * @since 0.1
	 */
	public void setAdapters(Iterable<Adapter<?, ?>> adapters) {
		container.setGraphs(
				new TransitiveClosure<Class<?>, Adapter<Object, Object>>(),
				new TransitiveClosure<Class<?>, Adapter<Object, Object>>());
		registerAdapters(adapters);
	}

	public void register(Adapter<?, ?> adapter) {
		container.register(adapter);
	}

	public Class<?>[] interfaces(Class<?> ... interfaces) {
		return container.interfaces(interfaces);
	}


	private class QueryAdapterContainer extends
			AdapterContainer<Query<Adapter<Object, Object>>> {

		protected QueryAdapterContainer(
				Graph<Class<?>, Query<Adapter<Object, Object>>> graph,
				Graph<Class<?>, Query<Adapter<Object, Object>>> implicitGraph) {
			super(graph, implicitGraph);
		}

		@Override
		protected Query<Adapter<Object, Object>> insertAdapter(Adapter adapter) {
			return new Query<Adapter<Object, Object>>(adapter);
		}
	}
}
